# -*- coding: utf-8 -*-
"""
    :author: Grey Li <withlihui@gmail.com>
    :copyright: (c) 2017 by Grey Li.
    :license: MIT, see LICENSE for more details.
"""
import os
import time

from flask import Flask, render_template, request,redirect,url_for
from flask_dropzone import Dropzone

basedir = os.path.abspath(os.path.dirname(__file__))


app = Flask(__name__)

app.config.update(
    UPLOADED_PATH=os.path.join(basedir, 'uploads'),
    # Flask-Dropzone config:
    DROPZONE_ALLOWED_FILE_CUSTOM=True,
    # DROPZONE_ALLOWED_FILE_TYPE='.pdf, .xlsx',
    DROPZONE_ALLOWED_FILE_TYPE='.pdf,.xlsx',
    DROPZONE_MAX_FILE_SIZE=30,
    DROPZONE_MAX_FILES=2,
    DROPZONE_UPLOAD_ON_CLICK=True,
    # DROPZONE_REDIRECT_VIEW='waiting_screen'
)
# app.config['DROPZONE_REDIRECT_VIEW'] = 'waiting_screen' #config端的优先级大于这里,BUG-->上传其他类型文件直接就跳转
dropzone = Dropzone(app)


@app.route('/', methods=['POST', 'GET'])
def upload():
    if request.method == 'POST':
        for key, f in request.files.items():
            if key.startswith('file'):
                f.save(os.path.join(app.config['UPLOADED_PATH'], f.filename))
                # time.sleep(3)
                # return 'wrong type',400
        # time.sleep(3)
        # return redirect('http://www.baidu.com')
    return render_template('index.html')

@app.route('/templates', methods=['POST', 'GET'])
def waiting_screen():
    return render_template("waiting_screen.html")

if __name__ == '__main__':
    app.run(debug=True)
